<?php
namespace app\api\controller;
use app\common\model\School as SchoolModel;
use app\admin\model\Config;
use think\Db;
use app\common\service\GuguService;


class Ranking extends App
{
   
	//获取查询条件
	public function get_condition(){
		
		$province_id = $this->province_id;
        if(empty($province_id)){
            return $this->show(2001,"缺少参数province_id!", '');
        }
        $year = $this->request->get("year",'','trim');
		if(empty($year)){
			$year = date('Y',strtotime("-1 year"));
		}
		$subject_mode = SchoolModel::getSubjectMode($province_id,$year);	
		if($subject_mode == 2){
			$type_list = ['综合'];
		}elseif($subject_mode == 3){
			$type_list = ['文科','理科'];
		}else{
			$type_list = ['物理类','历史类'];
		}
		$year_list = [];
		for($i=1;$i<4;$i++){
			$year_list[] = date('Y',strtotime("-$i year"));
		}
		
		$data = array('year_list'=>$year_list,'type_list'=>$type_list);
		return $this->show(1,"success", $data);
	}
	
	
	
	//位次查询
	public function index()
    {
		$pagesize = 100;
        $user_id = $this->user_id;
		$province_id = $this->province_id;
        if(empty($province_id)){
            return $this->show(2001,"缺少参数province_id!", '');
        }
		$year = $this->request->get("year",0,'intval');
		if(empty($year)){
			$year = date('Y',strtotime('-1 year'));
		}
		
		$member = Db::name("member")->field('province_id,cj_score,cj_subjects1,cj_subjects2,cj_subjects,cj_subjects,cj_subject_mode_title')->where('id',$user_id)->find();
        $type_name = $this->request->get("type_name",$member['cj_subject_mode_title'],'trim');
		$score = $this->request->get("score",$member['cj_score'],'intval');
        if(!empty($score)){
			if($score<150){
				return $this->show(2001,"没有找到该分数的对应位次，换个分数再试试吧", '');
			}
			if($province_id == 801){
				if($score>660){
					return $this->show(2001,"没有找到该分数的对应位次，换个分数再试试吧", '');
				}
			}else{
				if($score>750){
					return $this->show(2001,"没有找到该分数的对应位次，换个分数再试试吧", '');
				}
			}
        }
		$enrollprovince = Db::name("area")->where('id',$province_id)->value('name');
		
		$result = GuguService::make('/metadata/ceeline/one-score-one-section',['appkey'=>'CDZUAX7XAL84GKX9T96PULXVZMYLV8DQ','pageindex'=>1,'pagesize'=>$pagesize,'provincename'=>$enrollprovince,'subjectselection'=>$type_name,'year'=>intval($year)]);
		if($result[0] != 200){
            return $this->show(2002,$result[1]??'error', '');
		}
		$list = $result[1][1];
		$page_total = ceil($result[1][0]['DataTotalCount']/100);
		if($page_total>1){
			for($i=2;$i<=$page_total;$i++){
				$result = GuguService::make('/metadata/ceeline/one-score-one-section',['appkey'=>'CDZUAX7XAL84GKX9T96PULXVZMYLV8DQ','pageindex'=>$i,'pagesize'=>$pagesize,'provincename'=>$enrollprovince,'subjectselection'=>$type_name,'year'=>intval($year)]);
				if($result[0] != 200){
					return $this->show(2002,$result[1]??'error', '');
				}
				$list_temp = $result[1][1];
				$list = array_merge($list,$list_temp);
			}
		}
		
		
		/*$count = $result[1][0]['DataTotalCount'];
        $pages = ceil($count/$pagesize);
		if($pages>1){
			for($i=2;$i++;$i<=$pages){
				$result2 = GuguService::make('/metadata/ceeline/one-score-one-section',['appkey'=>'CDZUAX7XAL84GKX9T96PULXVZMYLV8DQ','pageindex'=>$i,'pagesize'=>$pagesize,'provincename'=>$enrollprovince,'subjectselection'=>$type_name,'year'=>intval($year)]);
				if($result2[0] != 200){
					return $this->show(2002,$result2[1]??'error', '');
				}
				$list2 = $result2[1][1];
				$list = array_merge($list,$list2);
			}
		}*/
		//dump($result);die;
		/*$line_data = array_map(function ($rs){
			$ExaminationScore = explode('-',$rs['ExaminationScore']);
			if(count($ExaminationScore)>1){
				$maxScore = $ExaminationScore[1];
				$minScore = $ExaminationScore[0];
			}else{
				$maxScore = $ExaminationScore[0];
				$minScore = $ExaminationScore[0];
			}
			$RankingRange = explode('-',$rs['RankingRange']);
			if(count($RankingRange)>1){
				$highestRank = $RankingRange[0];
				$LowestRank = $RankingRange[1];
			}else{
				$highestRank = $RankingRange[0];
				$LowestRank = $RankingRange[0];
			}
			return ["maxScore"=>$maxScore,'minScore' => $minScore,'sameCount'=>$rs['CandidateCount'],'highestRank'=>$highestRank,'LowestRank'=>$LowestRank
					];
           
        }, $list);*/
		$line_data = [];
		$one_score_data = [];
		$score_data = ['rank_range'=>'_','same_count'=>'_','rank'=>'_','competition_level'=>'_'];
		$history_year_data = [];
		//$batch_arr = array_column($list,'AdmissionBatchName','MinimumAdmissionScore');
		//dump($list);die;
		foreach($list as $k=>$rs){
			$ExaminationScore = explode('-',$rs['ExaminationScore']);
			if(count($ExaminationScore)>1){
				$maxScore = $ExaminationScore[1];
				$minScore = $ExaminationScore[0];
			}else{
				$maxScore = $ExaminationScore[0];
				$minScore = $ExaminationScore[0];
			}
			$RankingRange = explode('-',$rs['RankingRange']);
			if(count($RankingRange)>1){
				$highestRank = $RankingRange[0];
				$LowestRank = $RankingRange[1];
			}else{
				$highestRank = $RankingRange[0];
				$LowestRank = $RankingRange[0];
			}
			
			if($rs['MinimumAdmissionScore']>($minScore-1) && $rs['MinimumAdmissionScore']<=($maxScore+1)){
				$title = $rs['AdmissionBatchName'];
			}else{
				$title = '';
			}
			$line_data[$k] = ["maxScore"=>$maxScore,'minScore' => $minScore,'sameCount'=>$rs['CandidateCount'],'highestRank'=>$highestRank,'LowestRank'=>$LowestRank,"title"=>$title];
			$one_score_data[$k] = ["score"=>$rs['ExaminationScore'],'rank' => $rs['RankingRange'],'same_count'=>$rs['CandidateCount']];
			
			if(!empty($score)){
				if($score>($minScore-1) && $score<=($maxScore+1)){
					if($LowestRank<400){
						$competition_level = '低';
					}elseif($LowestRank>=400 && $LowestRank<700){
						$competition_level = '中';
					}else{
						$competition_level = '高';
					}
					$score_data = ['rank_range'=>$RankingRange,'same_count'=>$rs['CandidateCount'],'rank'=>$LowestRank,'competition_level'=>$competition_level];
					
					$history_year_data = $rs['HistoricalScores'];
				}
			}
		}
		
		//<400低 >700高
		
        $data = ['score_data'=>$score_data,'line_data'=>$line_data,'history_year_data'=>$history_year_data,'one_score_data'=>$one_score_data];
		return $this->show(1,"成功", $data);
		
    }
	
	
}